package com.aw.ist.analyzer;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
    public static final String TAG_NAME = "MainActivity";

    EditText editTextSE, editTextWA, editTextAN, editTextGE, editTextME, editTextRA, editTextZR, editTextFA, editTextWU;
    EditText editTextAnalytical, editTextConceptual, editTextNumerical, editTextSystematical, editTextCreativity;

    Button buttonCalculate;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        editTextSE = (EditText) findViewById(R.id.editTextSE);
        editTextWA = (EditText) findViewById(R.id.editTextWA);
        editTextAN = (EditText) findViewById(R.id.editTextAN);
        editTextGE = (EditText) findViewById(R.id.editTextGE);
        editTextME = (EditText) findViewById(R.id.editTextME);
        editTextRA = (EditText) findViewById(R.id.editTextRA);
        editTextZR = (EditText) findViewById(R.id.editTextZR);
        editTextFA = (EditText) findViewById(R.id.editTextFA);
        editTextWU = (EditText) findViewById(R.id.editTextWU);

        editTextAnalytical = (EditText) findViewById(R.id.editTextAnalytical);
        editTextConceptual = (EditText) findViewById(R.id.editTextConceptual);
        editTextNumerical = (EditText) findViewById(R.id.editTextNumerical);
        editTextSystematical = (EditText) findViewById(R.id.editTextSystematical);
        editTextCreativity = (EditText) findViewById(R.id.editTextCreativity);

        buttonCalculate = (Button) findViewById(R.id.buttonCalculate);

        buttonCalculate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i(TAG_NAME, "clicked button");
                try {
                    editTextAnalytical.setText(calculateAnalytic());
                    editTextConceptual.setText(calculateConceptual());
                    editTextNumerical.setText(calculateNumerical());
                    editTextSystematical.setText(calculateSystematical());
                    editTextCreativity.setText(calculateCreativity());
                } catch (Exception ex) {
                    Log.e(TAG_NAME, ex.getMessage());
                }
            }
        });


    }

    private CharSequence calculateCreativity() {
        Double ge = obtainGE();
        Double ra = obtainRA();
        Double an = obtainAN();
        Double zr = obtainZR();

        String mathOperator = (ge+ra) < (an+zr) ? "<" : ">";
        return String.format("GE+RA(%.2f) %s AN+ZR(%.2f)", ge+ra, mathOperator, an+zr);
    }

    private CharSequence calculateSystematical() {
        Double se = obtainSE();
        Double an = obtainAN();
        return String.format("%.2f",(se + an) / 2);
    }

    private CharSequence calculateNumerical() {
        Double ra = obtainRA();
        Double zr = obtainZR();
        return String.format("%.2f",(ra + zr)/2);
    }

    private CharSequence calculateConceptual() {
        Double fa = obtainFA();
        Double wu = obtainWU();
        Double ge = obtainGE();
        return String.format("%.2f",(fa + wu + ge)/3);
    }

    private CharSequence calculateAnalytic() {
        Double an = obtainAN();
        Double wu = obtainWU();
        return String.format("%.2f",(an + wu) /2);
    }

    private Double obtainSE() {
        String se = editTextSE.getText().toString();
        return Double.parseDouble(se == "" ? "0" : se);
    }

    private Double obtainWA() {
        String wa = editTextWA.getText().toString();
        return Double.parseDouble(wa == "" ? "0" : wa);
    }

    private Double obtainAN() {
        String an = editTextAN.getText().toString();
        return Double.parseDouble(an == "" ? "0" : an);
    }

    private Double obtainGE() {
        String ge = editTextGE.getText().toString();
        return Double.parseDouble(ge == "" ? "0" : ge);
    }

    private Double obtainME() {
        String me = editTextME.getText().toString();
        return Double.parseDouble(me == "" ? "0" : me);
    }

    private Double obtainWU() {
        String wu = editTextWU.getText().toString();
        return Double.parseDouble(wu == "" ? "0" : wu);
    }

    private Double obtainZR() {
        String zr = editTextZR.getText().toString();
        return Double.parseDouble(zr == "" ? "0" : zr);
    }

    private Double obtainRA() {
        String ra = editTextRA.getText().toString();
        return Double.parseDouble(ra == "" ? "0" : ra);
    }

    private Double obtainFA() {
        String fa = editTextFA.getText().toString();
        return Double.parseDouble(fa == "" ? "0" : fa);
    }
}
